<template>
	<view class="bodyBox-t30">
		<uni-forms ref="valiForm" :modelValue="user">
			<uni-forms-item label="头像" name="icon">
				<button class="buttonClear" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
					<image class="imgs border1" :src="icon"></image>
				</button>
			</uni-forms-item>
			<uni-forms-item label="昵称" name="name">
				<uni-easyinput type="nickname" v-model="user.name" placeholder="昵称" />
			</uni-forms-item>
			<uni-forms-item label="手机号" name="mobile">
				<uni-easyinput v-model="user.mobile" placeholder="请输入手机号" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" style="margin-top: 10px;" @click="submit('valiForm')">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				headers: {
					'access_token': '',
					'Content-Type': 'multipart/form-data'
				},
				icon: "",
				user: {},
			};

		},

		onLoad() {
			this.get();
			let token = uni.getStorageSync('token')
			this.headers.access_token = token;
		},
		methods: {
			get() {
				var that = this;
				that.apis.getUser().then(res => {
					that.icon = res.icon
					that.user = res
				})
			},
			submit(ref) {
				var that = this;
				that.$refs[ref].validate().then(res => {
					console.log('success', res);
					debugger
					that.user.icon = that.icon
					that.apis.userUpd(that.user).then(res => {
						uni.removeStorageSync("user")
						uni.showToast({
							title: "修改成功",
							icon: "none"
						})
						setTimeout(function() {
							uni.navigateBack()
						}, 500)
					})

				}).catch(err => {
					console.log('err', err);
				})
			},

			onChooseAvatar(e) {
				this.uploadFile(e.detail.avatarUrl);
			},
			uploadFile(path) {
				let that = this;
				uni.uploadFile({
					url: that.config.baseUrl + '/api/common/fileUpload', //后台接口
					//仅为示例，非真实的接口地址
					filePath: path,
					name: 'file',
					fileType: 'image',
					formData: {},
					header: that.headers, // header 值
					success: res => {
						let obj = JSON.parse(res.data)
						if (obj.code == 1) {
							let imgUrl = obj.data;
							this.icon = imgUrl
						} else {
							uni.showToast({
								icon: 'none',
								title: '上传失败！'
							});
						}
					},
					fail: e => {
						this.$toast('上传失败')
					}
				});
			},


		}

	}
</script>

<style lang="scss">

</style>
